package com.bruce.ai.alibaba.agentcard.discovery.circuitbreaker;

import java.time.Duration;

/**
 * 熔断器配置
 */
public class CircuitBreakerConfig {
    
    private final int failureThreshold;
    private final Duration waitDurationInOpenState;
    private final int permittedNumberOfCallsInHalfOpenState;
    private final Duration slidingWindowDuration;
    private final int minimumNumberOfCalls;
    private final double failureRateThreshold;
    
    private CircuitBreakerConfig(Builder builder) {
        this.failureThreshold = builder.failureThreshold;
        this.waitDurationInOpenState = builder.waitDurationInOpenState;
        this.permittedNumberOfCallsInHalfOpenState = builder.permittedNumberOfCallsInHalfOpenState;
        this.slidingWindowDuration = builder.slidingWindowDuration;
        this.minimumNumberOfCalls = builder.minimumNumberOfCalls;
        this.failureRateThreshold = builder.failureRateThreshold;
    }
    
    /**
     * 获取失败阈值
     */
    public int getFailureThreshold() {
        return failureThreshold;
    }
    
    /**
     * 获取开启状态下的等待时间
     */
    public Duration getWaitDurationInOpenState() {
        return waitDurationInOpenState;
    }
    
    /**
     * 获取半开状态下允许的调用次数
     */
    public int getPermittedNumberOfCallsInHalfOpenState() {
        return permittedNumberOfCallsInHalfOpenState;
    }
    
    /**
     * 获取滑动窗口持续时间
     */
    public Duration getSlidingWindowDuration() {
        return slidingWindowDuration;
    }
    
    /**
     * 获取最小调用次数
     */
    public int getMinimumNumberOfCalls() {
        return minimumNumberOfCalls;
    }
    
    /**
     * 获取失败率阈值
     */
    public double getFailureRateThreshold() {
        return failureRateThreshold;
    }
    
    /**
     * 创建默认配置
     */
    public static CircuitBreakerConfig defaultConfig() {
        return new Builder().build();
    }
    
    /**
     * 创建快速熔断配置
     */
    public static CircuitBreakerConfig fastConfig() {
        return new Builder()
                .failureThreshold(3)
                .waitDurationInOpenState(Duration.ofSeconds(10))
                .failureRateThreshold(0.3)
                .build();
    }
    
    /**
     * 创建慢熔断配置
     */
    public static CircuitBreakerConfig slowConfig() {
        return new Builder()
                .failureThreshold(10)
                .waitDurationInOpenState(Duration.ofMinutes(2))
                .failureRateThreshold(0.7)
                .build();
    }
    
    /**
     * 创建构建器
     */
    public static Builder builder() {
        return new Builder();
    }
    
    /**
     * 构建器类
     */
    public static class Builder {
        private int failureThreshold = 5;
        private Duration waitDurationInOpenState = Duration.ofSeconds(30);
        private int permittedNumberOfCallsInHalfOpenState = 3;
        private Duration slidingWindowDuration = Duration.ofMinutes(1);
        private int minimumNumberOfCalls = 10;
        private double failureRateThreshold = 0.5;
        
        /**
         * 设置失败阈值
         */
        public Builder failureThreshold(int failureThreshold) {
            if (failureThreshold <= 0) {
                throw new IllegalArgumentException("Failure threshold must be positive");
            }
            this.failureThreshold = failureThreshold;
            return this;
        }
        
        /**
         * 设置开启状态下的等待时间
         */
        public Builder waitDurationInOpenState(Duration waitDuration) {
            if (waitDuration == null || waitDuration.isNegative()) {
                throw new IllegalArgumentException("Wait duration must be positive");
            }
            this.waitDurationInOpenState = waitDuration;
            return this;
        }
        
        /**
         * 设置半开状态下允许的调用次数
         */
        public Builder permittedNumberOfCallsInHalfOpenState(int permittedCalls) {
            if (permittedCalls <= 0) {
                throw new IllegalArgumentException("Permitted calls must be positive");
            }
            this.permittedNumberOfCallsInHalfOpenState = permittedCalls;
            return this;
        }
        
        /**
         * 设置滑动窗口持续时间
         */
        public Builder slidingWindowDuration(Duration slidingWindowDuration) {
            if (slidingWindowDuration == null || slidingWindowDuration.isNegative()) {
                throw new IllegalArgumentException("Sliding window duration must be positive");
            }
            this.slidingWindowDuration = slidingWindowDuration;
            return this;
        }
        
        /**
         * 设置最小调用次数
         */
        public Builder minimumNumberOfCalls(int minimumCalls) {
            if (minimumCalls <= 0) {
                throw new IllegalArgumentException("Minimum calls must be positive");
            }
            this.minimumNumberOfCalls = minimumCalls;
            return this;
        }
        
        /**
         * 设置失败率阈值
         */
        public Builder failureRateThreshold(double failureRateThreshold) {
            if (failureRateThreshold < 0 || failureRateThreshold > 1) {
                throw new IllegalArgumentException("Failure rate threshold must be between 0 and 1");
            }
            this.failureRateThreshold = failureRateThreshold;
            return this;
        }
        
        /**
         * 构建配置
         */
        public CircuitBreakerConfig build() {
            return new CircuitBreakerConfig(this);
        }
    }
    
    @Override
    public String toString() {
        return "CircuitBreakerConfig{" +
                "failureThreshold=" + failureThreshold +
                ", waitDurationInOpenState=" + waitDurationInOpenState +
                ", permittedNumberOfCallsInHalfOpenState=" + permittedNumberOfCallsInHalfOpenState +
                ", slidingWindowDuration=" + slidingWindowDuration +
                ", minimumNumberOfCalls=" + minimumNumberOfCalls +
                ", failureRateThreshold=" + failureRateThreshold +
                '}';
    }
}